package org.lc.ktsdb

import org.lc.ktsdb.dsl.DemoScriptDeps
import java.io.File
import kotlin.script.experimental.api.EvaluationResult
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.onFailure
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate

fun main(args: Array<String>) {
    // println("Hello World!")

    // Try adding program arguments via Run/Debug configuration.
    // Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
    // println("Program arguments: ${args.joinToString()}")

    val scriptFile = File("kts/db.kts")
    evalFile(scriptFile).onFailure {
        println(it)
    }
    println("exec script finish")
}

fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
    val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<DemoScriptDeps>()
    return BasicJvmScriptingHost().eval(scriptFile.toScriptSource(), compilationConfiguration, null)
}

class DbAction {
    init {
        println("init")
    }

    fun test() {
        println("test")
    }

    fun test1(msg: String): String = "test1:${msg}"

    fun test2(action: DbAction.() -> String): String {
        println("test2")
        return this.action()
    }
}


val db = DbAction()

@JvmName("idOfSql")
fun id(idStr: String, action: DbAction.() -> String) {
    println(idStr)
    println(db.action())
}